********************************************************************************
* Purpose: Outcome for GP fees for child and mother at yearly level but excluding the first six months
********************************************************************************

do "D:\Data\workdata\708296\Project - Mother groups and mental health\Do\global.do" 

***************************************************************
*  appending raw data
**************************************************************

********************************************************************************
* Pick out GP services
********************************************************************************
* basic merge

forvalues i = 2009/2019 {
use "$raw\SSSY`i'", clear
keep pnr honuge kontakt bruhon speciale SPEC2
keep if SPEC2 == "80" | SPEC2 == "81" | SPEC2 == "82" | SPEC2 == "83" | SPEC2 == "89"
compress
save "$work\GP`i'", replace		
}
forvalues i = 2020/2020 {
use "$raw\SSSY`i'", clear
keep pnr honuge kontakt bruhon speciale SPEC2
tostring SPEC2, replace
tostring speciale, replace
tostring pnr, replace format("%12.0f")
tostring honuge, replace
replace pnr = substr("000000000000",1,12 - length(pnr)) + pnr if pnr != "."
replace pnr = "" if pnr == "."
keep if SPEC2 == "80" | SPEC2 == "81" | SPEC2 == "82" | SPEC2 == "83" | SPEC2 == "89"
compress
save "$work\GP`i'", replace		
}


* Merge with kids
forvalues i = 2012/2020 {
use "$work\GP`i'",clear	
merge m:1 pnr using "$work\mfr_kids", keep(3) nogen
save "$work\GP`i'merged_pnr",replace
}
use "$work\GP2012merged_pnr",clear
forvalues i = 2013/2020 {
append using "$work\GP`i'merged_pnr"		
}
save "$work\GPkids_1220", replace

* Merge with mothers
forvalues i = 2011/2020 {
forvalues j = 1/5 {
use "$work\moms_no`j'",clear
rename pnr prnb
rename id_mother pnr
merge 1:m pnr using "$work\GP`i'", keep(3) nogen
save "$work\GP`i'merged_cprm`j'",replace
}	
}
forvalues i = 2011/2020 {
use "$work\GP`i'merged_cprm1"
forvalues j = 2/5 {
append using "$work\GP`i'merged_cprm`j'"
}
save "$work\GP`i'merged_cprm", replace
}
use "$work\GP2011merged_cprm",clear
forvalues i = 2012/2020 {
append using "$work\GP`i'merged_cprm", force
}
save "$work\GP_cprm", replace



********************************************************************************
* Generate outcomes
* kids 
********************************************************************************
use "$work\GPkids_1220", clear
keep if SPEC2 == "80"
gen int birthweek = week(mfr_dob)
gen int birthyear = mfr_yob
gen gpweek = substr(honuge, -2,2)
destring gpweek, replace

gen gpyear = substr(honuge,1,2)
destring gpyear, replace
forvalues i= 12/20 {
replace gpyear = 2000 + `i' if gpyear == `i'	
}
gen fourdigit = substr(speciale,-4,4)
destring fourdigit, replace

gen honorar = .
replace honorar = bruhon
replace honorar = 0 if honorar < 0
sort pnr gpweek gpyear speciale
by pnr gpweek gpyear speciale: drop if _n > 1
by pnr gpweek gpyear: egen sumhonorar = sum(honorar)

gen gp_date = mdy(1,1,gpyear) + (gpweek - 1)*7
format gp_date %td

drop if gp_date - mfr_dob < 182
gen age_at_contact = floor((gp_date - mfr_dob)/365.25)

drop if age_at_contact > 3

collapse (sum) sumhonorar, by(pnr age_at_contact) fast
levelsof age_at_contact, local(levels)
reshape wide sumhonorar, i(pnr) j(age_at_contact)

foreach i of local levels {
replace sumhonorar`i' = 0 if sumhonorar`i' == .	
}
compress
save "$work\gp_honorar", replace


****************************
* mothers
****************************
use "$work\GP_cprm", clear

keep if SPEC2 == "80"
gen int birthweek = week(mfr_dob)
gen int birthyear = mfr_yob
gen gpweek = substr(honuge, -2,2)
destring gpweek, replace

gen gpyear = substr(honuge,1,2)
destring gpyear, replace
forvalues i= 12/20 {
replace gpyear = 2000 + `i' if gpyear == `i'	
}
gen honorar = .
replace honorar = bruhon
replace honorar = 0 if honorar < 0
rename pnr cprm
sort cprm mfr_dob gpweek gpyear speciale
by cprm mfr_dob gpweek gpyear speciale: drop if _n > 1
by cprm mfr_dob gpweek gpyear: egen sumhonorar = sum(honorar)

gen gp_date = mdy(1,1,gpyear) + (gpweek - 1)*7
format gp_date %td

gen age_at_contact = floor((gp_date - mfr_dob)/365.25)
drop if age_at_contact < 0
drop if age_at_contact > 2

collapse (sum) sumhonorar, by(cprm mfr_dob age_at_contact) fast
levelsof age_at_contact, local(levels)
reshape wide sumhonorar, i(cprm mfr_dob) j(age_at_contact)

foreach i of local levels {
replace sumhonorar`i' = 0 if sumhonorar`i' == .	
rename sumhonorar`i' sumhonorarm`i'
}
compress
save "$work\gpm_honorar", replace
